package learn_effective_java.item_02.nutritionfacts;
/**
 * Item 2: Consider a builder when faced with many constructor parameters.
 * 
 * Static factories and constructors share a limitation:
 * they do not scale well to large numbers of optional parameters.
 * 
 * Consider the case of a class representing the Nutrition Facts label
 * that appears on packaged foods.
 * 
 * These labels have a few required fields--
 * serving size, 
 * servings per container, 
 * and calories per serving--
 * and more than twenty optional fields--
 * total fat, saturated fat, trans fat,
 * cholesterol, sodium, and so on.
 * 
 * Most products have nonzero values for only a few of these optional fields.
 * 
 * What sort of constructors or static factories should you write for such a class?
 * Traditionally, programmers have used the telescoping constructor pattern, 
 * in which you provide a constructor with only the required parameters, 
 * another with a single optional parameter, 
 * a third with two optional parameters, 
 * and so on,
 * culminating in a constructor with all the optional parameters.
 * 
 * Here's how it looks in practice.
 * For brevity's sake, only four optional fields are shown:
 *
 */
public class NutritionFacts1 {
	
	private final int servingSize;
	private final int servings;
	private final int calories;
	private final int fat;
	private final int sodium;
	private final int carbohydrate;
	
	public NutritionFacts1(int servingSize, int servings) {
		this(servingSize, servingSize, 0);
	}
	
	public NutritionFacts1(int servingSize, int servings, int calories) {
		this(servingSize, servings, calories, 0);
	}
	
	public NutritionFacts1(int servingSize, int servings, int calories, int fat) {
		this(servingSize, servings, calories, fat, 0);
	}
	
	public NutritionFacts1(int servingSize, int servings, int calories, int fat, int sodium) {
		this(servingSize, servings, calories, fat, sodium, 0);
	}
	
	public NutritionFacts1(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
		this.servingSize = servingSize;
		this.servings = servings;
		this.calories = calories;
		this.fat = fat;
		this.sodium = sodium;
		this.carbohydrate = carbohydrate;
	}
	
	public static void main(String[] args) {
		/*
		 * When you want to create an instance, you use the constructor with 
		 * the shortest parameter list containing all the parameters you want to set:
		 */
		NutritionFacts1 cocaCola = new NutritionFacts1(240, 8, 100, 0, 35, 27);
		
		/*
		 * Typically this constructor invocation will require many parameters 
		 * that you don't want to set, 
		 * but you're forced to pass a value for them anyway.
		 * 
		 * In this case, we passed a value of 0 for fat.
		 * With "only" six parameters this may not seem so bad,
		 * but it quickly gets out of hand as the number of parameters increases.
		 */
		
		/*
		 * In short, the telescoping constructor pattern works, 
		 * but it is hard to write client code when there are many parameters, 
		 * and harder still to read it.
		 * 
		 * The reader is left wondering what all those values mean 
		 * and must carefully count parameters to find out.
		 * 
		 * Long sequences of identically typed parameters can cause subtle bugs.
		 * 
		 * If the client accidentally reverses two such parameters, 
		 * the compiler won't complain, but the program will misbehave at runtime.
		 */
	}
	
}






















